package com.dj.mingkong.portal.biz.controller.music;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.mingkong.common.annotation.RequestLimit;
import com.dj.mingkong.common.infrastructure.base.dto.page.PageVo;
import com.dj.mingkong.common.infrastructure.base.dto.rest.RestResponse;
import com.dj.mingkong.common.microservice.UserContext;
import com.dj.mingkong.common.util.AssertUtils;
import com.dj.mingkong.common.util.SmartBeanCopy;
import com.dj.mingkong.portal.biz.controller.music.vo.request.MusicVisitAddVo;
import com.dj.mingkong.portal.biz.controller.music.vo.request.MusicVisitCancelVo;
import com.dj.mingkong.portal.biz.controller.music.vo.request.MusicVisitQueryVo;
import com.dj.mingkong.portal.biz.controller.music.vo.response.MusicVisitVo;
import com.dj.mingkong.portal.biz.service.music.MusicVisitService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 音乐访客管理
 *
 * @author zenglonghui
 * @date: 2022/8/23 10:57
 */
@RestController
@RequestMapping("/music/visit")
public class MusicVisitController {
    @Resource
    private MusicVisitService musicVisitService;

    /**
     * 新增音乐访客与人气
     *
     * @param addVo 实例对象
     * @return 是否成功，true-成功，false-失败
     * @throws RuntimeException addPopularityVo.getId为空的时候会抛出运行时异常
     */
    @PostMapping("/add")
    public RestResponse<Boolean> add(@RequestBody @Validated MusicVisitAddVo addVo) {
        musicVisitService.add(addVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 分页获取我听过的歌曲列表
     *
     * @param queryVo {}
     * @return MusicVisitVo
     */
    @PostMapping("/pageByVo")
    public RestResponse<PageVo<MusicVisitVo>> pageByVo(@RequestBody MusicVisitQueryVo queryVo) {
        AssertUtils.checkQueryPageVo(queryVo);
        queryVo.setUserId(UserContext.getUserId());
        final Page<MusicVisitVo> musicVisitVoPage = musicVisitService.pageByVo(queryVo);
        final PageVo<MusicVisitVo> pageVo = SmartBeanCopy.copyPageVo(musicVisitVoPage, MusicVisitVo.class);
        return RestResponse.success(pageVo);
    }

    /**
     * 取消到访记录
     *
     * @param cancelVo {}
     * @return Boolean
     */
    @PostMapping("/cancel")
    @RequestLimit(count = 5)
    public RestResponse<Boolean> cancel(@RequestBody @Validated MusicVisitCancelVo cancelVo) {
        musicVisitService.cancel(cancelVo);
        return RestResponse.buildSuccess();
    }
}
